module ActiveBackground
  
  # some methods to convert the keys from symbols to strings
  # and back again....
  class Configuration
    def load(file)
      config = YAML.load_file(file)
      config.each do |key, value|
        unless key.is_a?(Symbol)
          config[key.to_sym] = value
          config.delete(key)
        end
      end
    end
    
    def dump(config)
      config.each do |key, value|
        unless key.is_a?(String)
          config[key.to_s] = value
          config.delete(key)
        end
      end
    end
    
    def installed_as_rails_plugin?
      if File.exist?(File.dirname(__FILE__) + "/../../../../config/boot.rb")
        true
      else
        false
      end
    end
    
    def self.setup(service = "Base")
      server = Server.new
      config = Configuration.new
      options = server.defaults
      config_file = options[:config_file]
      options[:service] = service
      options.delete(:config_file)
      
      if config.installed_as_rails_plugin?
        config.initialize_rails(:boot)
        root = RAILS_ROOT
      else
        root = File.dirname(__FILE__) + "/../.."
      end
      
      begin
        if !File.exist?(root + "/config/#{config_file}")
          File.open(root + "/config/#{config_file}", "w+") {|f| YAML.dump(config.dump(options), f)}
        else
          if !YAML.load_file(root + "/config/#{config_file}")
            File.open(root + "/config/#{config_file}", "w+") {|f| YAML.dump(config.dump(options), f)}
          else
            tmp_options = options.merge(config.load(root + "/config/#{config_file}"))
            File.open(root + "/config/#{config_file}", "w+") {|f| YAML.dump(config.dump(tmp_options), f)}
          end
        end
      rescue Exception => e
        puts 'setup failed ' + e.to_s
      end
    end
    
    def self.load
      server = Server.new
      config = Configuration.new
      options = server.defaults
      
      begin
        begin # try to load rails
          require File.dirname(__FILE__) + "/../../../../config/boot.rb"
          options = options.merge(config.load("#{RAILS_ROOT}/config/#{options[:config_file]}"))
        rescue Exception => e # loading of rails failed
          options = options.merge(config.load(File.dirname(__FILE__) + "/../config/#{options[:config_file]}"))
        end
      rescue Exception => e # loading of local file failed
        options = options
      end
      
      return options
    end
    
    def initialize_rails(level = :boot)
      case level
        when :boot
          load_rails_config
        when :active_record
          load_rails_config
          load_active_record
        when :all
          load_rails_config
          load_active_record
          load_action_pack
      end
    end
    
    private 
      def load_rails_config
        begin
          if File.exist?(File.dirname(__FILE__) + "/../../../../config/boot.rb")
            require File.dirname(__FILE__) + "/../../../../config/boot.rb"
          else
            raise 'rails distribution not found or not installed as plugin'
          end
        rescue
          # log something
        end
      end
      
      def load_active_record
        
      end
      
      def load_action_pack
        
      end
  end
end